package com.ruoyi.web.controller.system.fuw.wx;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.fuw.domain.FuwCar;
import com.ruoyi.fuw.domain.FuwComm;
import com.ruoyi.fuw.service.IFuwCarService;
import com.ruoyi.fuw.service.IFuwCommService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Wx商品信息Controller
 *
 * @author zjh
 * @date 2021-10-09
 */
@RestController
@RequestMapping("/api/wx/car")
public class WxFuwCarController extends BaseController {

    @Autowired
    private IFuwCarService fuwCarService;

    @Autowired
    private IFuwCommService fuwCommService;

    /**
     * 通过commList的commId获取商品的详细信息
     */
    @PreAuthorize("@ss.hasPermi('fuw:comm:list')")
    @GetMapping("/list")
    public AjaxResult listCarComm(FuwCar fuwCar)
    {
        fuwCar.setPersonId(getUsername());
        List<FuwCar> listCar = fuwCarService.selectFuwCarList(fuwCar);

        // 定义商品详情List
        List<FuwComm> fuwCommList = new ArrayList<>();

        for(FuwCar car: listCar){
            FuwComm fuwComm = fuwCommService.selectFuwCommByCommId(car.getCommId());
            fuwCommList.add(fuwComm);
        }

        return AjaxResult.success(fuwCommList);
    }

    /**
     * 删除购物车
     */
    @PreAuthorize("@ss.hasPermi('fuw:car:remove')")
    @GetMapping("/delete/commId")
    public AjaxResult deleteChatByCommId(String commId)
    {
        FuwCar fuwCar = new FuwCar();
        fuwCar.setCommId(commId);
        fuwCar.setPersonId(getUsername());
        return toAjax(fuwCarService.deleteFuwCarByCommId(fuwCar));
    }

}
